package com.plantplaces.ui;

import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.List;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.ImageView;

import com.example.fs131046plantplaces.R;
import com.plantplaces.dao.IPlantDAO;
import com.plantplaces.dao.OfflinePlantDAO;

/**
 * Collect search information for a plant.
 * 
 * @author jonesbr
 *
 */
public class AdvancedSearchActivity extends PlantPlacesActivity {
	
	private static final int CAMERA_REQUEST = 2;

	private static final int IMAGE_GALLERY_REQUEST = 1;

	public static final String PLANT_SEARCH_NAME = "PLANT_SERCH_NAME";

	// create a variable to store the autocomplete view for use in other methods.
	AutoCompleteTextView actPlantName;	
	
	private IPlantDAO plantDAO;

	private Bitmap image;

	private ImageView thumbnail;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
		// Associate this activity with its layout.
		setContentView(R.layout.search);
		
		
		// get a reference to the views on the layout.
		actPlantName = (AutoCompleteTextView) findViewById(R.id.actPlantName);
		
		// Create a PlantDAOStub object and assign it to our plantDAO variable.
		plantDAO = new OfflinePlantDAO(this);
		
		try {
			// get a list of unique plant names.
			List<String> plantNames = plantDAO.fetchPlantNames();
			
			// create an ArrayAdapter that will allow us to use these unique names in a Spinner.
			ArrayAdapter<String> aaPlantNames = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, plantNames);
			
			// associate this adapter with the actPlantNames View.
			actPlantName.setAdapter(aaPlantNames);
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		thumbnail = (ImageView) findViewById(R.id.imgThumbnail);
		
		
	}
	
	/**
	 * This method will be invoked when the search button is clicked.
	 * @param v
	 */
	public void onSearchClicked(View v) {
		
		// get the text that the user entered into the plant name field.
		String plantName = actPlantName.getText().toString();
		
		// make an intent that will display the results screen.
		Intent plantResultsIntent = new Intent(this, PlantResultActivity.class);
		
		// provide information to the following activity.
		plantResultsIntent.putExtra(PLANT_SEARCH_NAME, plantName);
		
		// go to the new intent.
		startActivity(plantResultsIntent);
	}
	
	/**
	 * Show a gallery view of images that the user can select.
	 * @param v
	 */
	public void onOpenExistingImageClick(View v) {
		// we want to pick something.
		Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
		// find the common area where images are stored.
		Uri imageLocation = Uri.parse(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getPath());
		// what type of data do we want to find?
		String type = "image/*";
		// assemble this all together
		photoPickerIntent.setDataAndType(imageLocation, type);
		
		// start this activity.
		startActivityForResult(photoPickerIntent, IMAGE_GALLERY_REQUEST);
		
		
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if(resultCode == RESULT_OK) {
			// RESULT_OK means that everything processed successfully.
			
			if (requestCode == IMAGE_GALLERY_REQUEST) {
				// this request code means that the user chose to open the gallery view.
				
				// find the full file path of the inmage file that the user chose.
				Uri imageUri = data.getData();
				try {
					// open the full file path as a stream of data.
					InputStream inputStream = getContentResolver().openInputStream(imageUri);
					
					// take a stream of data from a file, and turn it into a bitmap object.
					image = BitmapFactory.decodeStream(inputStream);
					
					// show the image.
					thumbnail.setImageBitmap(image);
					
				} catch (FileNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			} else if (requestCode == CAMERA_REQUEST) {
				// hande a response from the camera.
				
				// get the image data from the camera.
				image = (Bitmap) data.getExtras().get("data");
				
				// show the image.
				thumbnail.setImageBitmap(image);
			}
		}
	
	}
	
	
	public void onTakeNewPhotoClick(View v) {
		Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
		startActivityForResult(cameraIntent, CAMERA_REQUEST);
	}

}
